package com.jinmei.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jinmei.dao.ToDoListDao;
import com.jinmei.model.Employee;
import com.jinmei.model.ToDoList;

public class ToDoListDaoImpl extends HibernateDaoSupport implements ToDoListDao {

	@Override
	public void saveOrUpdateToDoList(ToDoList toDo) throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(toDo);
	}

	@Override
	public void deleteToDoList(Integer toDoId) throws DataAccessException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ToDoList.class);
		criteria.add(Restrictions.eq("id", toDoId));
		List<ToDoList> toDoTasks = this.getHibernateTemplate().findByCriteria(criteria);
		this.getHibernateTemplate().delete(toDoTasks.get(0));		
	}

	@Override
	public List<ToDoList> getTasksByEmployee(Employee employee) throws DataAccessException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ToDoList.class);
		criteria.add(Restrictions.eq("employee", employee));
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

}
